/*
 * @lc app=leetcode id=94 lang=cpp
 *
 * [94] Binary Tree Inorder Traversal
 */
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    vector<int> inorderTraversal(TreeNode* root) {
        vector<int> ret;
        if (!root) {
            return ret;
        }

        vector<TreeNode *> path;
        TreeNode *node = root;
        while(node || !path.empty()) {

            if (!node) {
                node = path.back();
                path.pop_back();

                ret.push_back(node->val);
                node = node->right;
            }

            while(node && node->left) {
                path.push_back(node);
                node = node->left;
            }

            if (node) {
                ret.push_back(node->val);
                node = node->right;
            }

        }

        return ret;
        
    }
};

